/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.db;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.Assert;

public class DbKit {

	/**
	 * The main Config object for system
	 */
	static Config config = null;

	private static Map<String, Config> configNameToConfig = new HashMap<String, Config>();

	public static final String MAIN_CONFIG_NAME = "main";

	private DbKit() {}

	/**
	 * Add Config object
	 * @param config the Config contains DataSource, Dialect and so on
	 */
	public static void addConfig(Config config) {
		Assert.notNull(config, "Config can not be null");
		Assert.isTrue(!configNameToConfig.containsKey(config.getName()), "Config already exists: " + config.getName());

		configNameToConfig.put(config.getName(), config);

		/** 
		 * Replace the main config if current config name is MAIN_CONFIG_NAME
		 */
		if (MAIN_CONFIG_NAME.equals(config.getName())) DbKit.config = config;

		/**
		 * The configName may not be MAIN_CONFIG_NAME,
		 * the main config have to set the first comming Config if it is null
		 */
		if (DbKit.config == null) DbKit.config = config;
	}

	public static Config getConfig() {
		return config;
	}

	public static Config getConfig(String configName) {
		return configNameToConfig.get(configName);
	}

	public static Config getConfig(SqlSessionFactory sqlSessionFactory) {
		for (Config config : configNameToConfig.values()) {
			if (config.getSqlSessionFactory().equals(sqlSessionFactory)) return config;
		}
		return null;
	}

	public static Config getConfig(Configuration configuration) {
		for (Config config : configNameToConfig.values()) {
			if (config.getSqlSessionFactory().getConfiguration().equals(configuration)) return config;
		}
		return null;
	}
}
